Skip to content

Build the MCP tool context from a session in one place - #1144

Open
richshaw wants to merge 2 commits into
mainfrom
claude/issue-1141-bearer-organizer
Open

richshaw wants to merge 2 commits into
mainfrom
claude/issue-1141-bearer-organizer

Conversation

@richshaw

@richshaw richshaw commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Fixes #1141.
Session: https://claude.ai/code/session_016YZ9SSbFFg2F4VY28wUau4

The problem

The code that turns a signed-in organizer into the context an MCP tool runs with was written out three times. resolveBearerActor built it in production, contextForOrganizer (used by the MCP DB tests and the follow-up evals) rebuilt it field by field, and the route copied the six fields into ToolContext one at a time. BearerWorkspace and ToolWorkspace were the same type. If a field was added to the bearer path but missed in the test copy, the MCP tests would keep running on a context production no longer builds.

The fix

  • src/auth/bearer.ts: a new exported BearerOrganizer type (actor, scopes, clientId, defaultWorkspaceId, workspaces, locale) and a pure bearerOrganizer(session, clientId, scopes). It calls toActor, narrows the kind, copies the actor with via (the comment about not mutating the React-cached actor moved with it), and maps the memberships. It replaces bearerWorkspaces. BearerResolution's ok branch is { ok: true; organizer: BearerOrganizer; authInfo }, so the organizer and the SDK's auth info, which carries the raw token, stay apart. BearerOrganizer's doc comment says that every field on it reaches every tool. The seam comment sits on resolveBearerActor.
  • src/mcp/context.ts: ToolContext is BearerOrganizer & { db }. ToolWorkspace is gone. The only new import is a type-only BearerOrganizer from the seam module, and tool handlers still import nothing from @/auth.
  • src/mcp/testing/context.ts: loads the session, calls bearerOrganizer, and throws if it gives null.
  • The route builds the context as { db: getDb(), ...auth.organizer } and hands auth.authInfo to the SDK on its own, so a field reaches tools only once it's added to BearerOrganizer (owner's review).

No behaviour changes, so there's no CHANGELOG entry. It touches src/auth/ and the MCP route, so it waits for the owner. #1135 (from a fork) changes key-error logging in bearer.ts. The lines don't overlap, but whichever lands second needs a small merge.

Tests

  • route.test.ts checks that the context the tools receive has exactly the expected keys, with no authInfo and no copy of the token, even when the resolution carries an extra field. With the old spread in the route, it fails.
  • tools.db.test.ts and the OAuth flow DB tests run through contextForOrganizer and resolveBearerActor.
  • grep -rn "ToolWorkspace\|bearerWorkspaces" src finds nothing.
  • pnpm lint && pnpm format:check && pnpm typecheck && pnpm knip && pnpm test pass (3895 tests).
  • pnpm test:db passes.

🤖 Generated with Claude Code

https://claude.ai/code/session_016YZ9SVvPGrXSNjSDiwe5Ch

resolveBearerActor and the MCP tests' contextForOrganizer each turned an
organizer's session into the tool context by hand, and the route copied
the fields over once more. They now share bearerOrganizer in the bearer
seam, ToolContext is its type plus db, and the route spreads it, so a
field added to the bearer path can't be missed by the tests and evals.

Fixes #1141.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016YZ9SSbFFg2F4VY28wUau4
@richshaw richshaw added pipeline:tophat pipeline:working A session is working on it right now and removed pipeline:working A session is working on it right now labels Nov 27, 2026 — with Claude

Copy link
Copy Markdown
Owner Author

Tophat: passed

See it: https://claude.ai/artifact/7Wf3Ne1VPv5k821mqtju7T
What changes for people: Nothing. A connected AI assistant sees the same tools, answers and refusals as before. Only the way the server works out who a token belongs to has been tidied up.

Check Result What I saw
Same MCP calls on base and head ✅ Real token from the consent screen. tools/list (18), list_workspaces, list_signups, create_signup and get_signup gave identical normalised results on both commits, apart from the signup count, which the first run raised by one.
Organizer's language reaches tools ✅ With the account set to Swedish, create_signup with no locale made a sv signup, before and after.
Client recorded in activity ✅ The signup.created row carries viaClientId: e2e-client, the same on both commits.
Viewer in a second workspace ✅ list_workspaces shows owner and viewer roles. create_signup into the viewer workspace is refused with forbidden.
Token whose organizer was deleted ✅ Got a token, deleted the organizer, called again: 401 invalid_token "unknown account". This is the branch the PR rewrote.
No token, bad token, missing scope ✅ 401 with resource_metadata, 401 invalid_token, and 403 insufficient_scope (signups:write) for a read-only token. Same before and after.

Worth a look: the route now spreads the whole resolution, minus ok and authInfo, into the tool context. A field added to the resolution later reaches tools automatically. That's the point of the change, but a secret added there would reach them too.

Not checked on the base: the viewer and deleted-organizer cases. I ran them on the head only, and their wording comes from code this PR doesn't touch.

Ran 09a688e against a820439 locally, MCP over HTTP with a token from the real OAuth flow, made-up data. https://claude.ai/code/session_016YZ9STFWdWM98i7mGfTd2w


Generated by Claude Code

@richshaw richshaw added pipeline:review pipeline:working A session is working on it right now and removed pipeline:tophat pipeline:working A session is working on it right now labels Nov 27, 2026 — with Claude

@richshaw richshaw left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: passed (round 1)

bearerOrganizer is now the only place a session becomes a tool context. resolveBearerActor, contextForOrganizer and the route all go through it, and they build the same fields as before, including the 401 for an organizer who is gone. Nothing in src still uses ToolWorkspace or bearerWorkspaces. Both "Done when" items are met, and so is the issue's outline.

Blocking (0)
Optional (3):

  • route.ts:109: the context now takes everything except ok and authInfo, so any field added later to the resolution reaches every tool. The tophat flagged this too. Returning { ok, organizer, authInfo } would make adding a field a deliberate choice.
  • bearer.ts:24: the file's seam comment now sits on the exported BearerOrganizer, so it shows as that type's hover doc. Moving it onto resolveBearerActor would fix that.
  • context.ts:1: the type-only BearerOrganizer import is what the issue planned. CLAUDE.md's "only auth import" line could name it.

Earlier threads: none
Checks: CI is green on 09a688e (lint-type-test, e2e, CodeQL). The tophat passed on this SHA. I ran code-review at high effort.

This waits for you because it touches src/auth/bearer.ts and the MCP route, which are high-risk. It changes no behaviour. Card: https://claude.ai/artifact/7Wf3Ne1VPv5k821mqtju7T

https://claude.ai/code/session_016YZ9SVJHryH4EUPxis53cd


Generated by Claude Code

@richshaw richshaw added pipeline:owner Passed; waits for the owner (UX or high-risk) and removed pipeline:review pipeline:working A session is working on it right now labels Nov 27, 2026 — with Claude

@richshaw richshaw left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sending this back for one change, in the thread on route.ts. Everything else looks right: an independent security review found authorization unchanged (actor, scopes, workspaces, roles and the 401 for a missing organizer) and nothing sensitive reaching tools today.

Optional, if it's quick while you're in there: move the file's seam comment off BearerOrganizer back onto resolveBearerActor (it now shows as the type's hover doc), and add a line to BearerOrganizer's doc comment saying every field on it reaches every tool, so never put token material there.

https://claude.ai/code/session_016YZ9SVfNPHkNsZgUBaTPSS


Generated by Claude Code

Comment thread src/app/api/mcp/route.ts Resolved
@richshaw richshaw added pipeline:fix pipeline:working A session is working on it right now and removed pipeline:owner Passed; waits for the owner (UX or high-risk) labels Nov 28, 2026 — with Claude
resolveBearerActor now returns { ok, organizer, authInfo }, and the route
builds the tool context from auth.organizer alone, so a field reaches
tools only once it is added to BearerOrganizer. The route test checks the
context has exactly the expected keys and no auth info, even when the
resolution carries something extra. The seam comment moves back onto
resolveBearerActor, and BearerOrganizer's says that every field reaches
every tool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016YZ9SVvPGrXSNjSDiwe5Ch
@richshaw richshaw added pipeline:tophat pipeline:working A session is working on it right now and removed pipeline:fix pipeline:working A session is working on it right now labels Nov 28, 2026 — with Claude

Copy link
Copy Markdown
Owner Author

Tophat: passed

See it: https://claude.ai/artifact/7Wf3Ne1VPv5k821mqtju7T
What changes for people: Nothing. A connected AI assistant sees the same tools, answers and refusals as before. This round re-checks the head after the owner's review, now that tools get only the organizer fields by name.

Check Result What I saw
Same MCP calls on base and head ✅ Real tokens from the consent screen. tools/list (18), list_workspaces, list_signups, create_signup and get_signup gave identical normalised results on both commits, apart from the title's run label and the signup count.
Organizer's language reaches tools ✅ With the account set to Swedish, create_signup with no locale made a sv signup, before and after.
Client recorded in activity ✅ The signup.created row carries viaClientId: e2e-client on both commits.
Viewer in a second workspace ✅ list_workspaces shows owner and viewer roles. create_signup into the viewer workspace is refused with forbidden, on both commits.
Token whose organizer was deleted ✅ Got a token, deleted the organizer, called again: 401 invalid_token "unknown account", on both commits.
No token, bad token, missing scope ✅ 401 with resource_metadata, 401 invalid_token, and 403 insufficient_scope (signups:write) for a read-only token. Same before and after.

This time the viewer and deleted-organizer cases also ran on the base, which the first tophat didn't do.

Ran 154b298 against a820439 locally, MCP over HTTP with tokens from the real OAuth flow, made-up data. https://claude.ai/code/session_016YZ9SW6QmGhHSwuvQcJpBm


Generated by Claude Code

@richshaw richshaw added pipeline:review and removed pipeline:tophat pipeline:working A session is working on it right now labels Nov 28, 2026 — with Claude
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The MCP tool context is built from a session twice, so the MCP DB tests and evals run on a copy of production's code

2 participants